package com.example.springbootsecurity.service;

import com.example.springbootsecurity.domain.SysUser;
import com.example.springbootsecurity.repository.ISysUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * 自定义需实现 UserDetailsService
 * @author ljw
 * @date 2022/8/17 7:50
 */
@Service
public class CustomUserService implements UserDetailsService {

    @Autowired
    private ISysUserRepository sysUserRepository;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = sysUserRepository.findByUsername(username);
        if (null == sysUser) {
            throw new UsernameNotFoundException("用户名不存在");
        }
        return sysUser;
    }
}
